<template>
  <div class="ele-body">
    <el-card shadow="never" body-style="padding: 25px;">
      <div slot="header" class="title" >
        <span>开发者配置</span>
      </div>
      <el-row :gutter="15">
        <el-col v-bind="styleResponsive ? { md: 24, sm: 24 } : { span: 24 }">
          <el-form
            ref="form"
            :model="form"
            :rules="rules"
            label-position="top"
            label-width="130px"
            style="padding: 34px 20px 0 20px"
            @keyup.enter.native="save"
            @submit.native.prevent
          >
            <el-row :gutter="15">
              <el-col :sm="24">
                <el-form-item label="APP ID:" prop="appid">
                  <el-input
                  disabled
                  v-model="form.appid"
                  placeholder="APP ID"
                  clearable
                  />
                </el-form-item>
                <el-form-item label="APP 秘钥:" prop="appsecret">
                  <el-input disabled v-model="form.appsecret" placeholder="APP 秘钥" clearable>
                    <template slot="append" v-if="form.appsecret">
                      <el-button type="primary" style="backgroundColor:#1890ff;color:#ffffff;" @click="resetKey()">重新生成</el-button>
                    </template>
                  </el-input>

                </el-form-item>
                
              </el-col>
            </el-row>

            <div class="ele-bottom-tool" v-if="form.is_status == 2">
              <div class="ele-bottom-tool-actions">
                <el-button type="primary" :loading="loading" @click="save">
                  提交
                </el-button>
              </div>
            </div>
            
          </el-form>
        </el-col>
      </el-row>
    </el-card> 
   
  </div>
</template>

<script>
  import { info,edit,resetKey } from '@/api/account/list';

  export default {
    name: 'AccountConfig',
    data() {
      return {
        // 表单数据
        form: {
          appid: '',
          appsecret: '',
          domain: '',
        },
        // 表单验证规则
        rules: {
          appid: [
            {
              required: true,
              message: '请输入appid',
              trigger: 'blur'
            }
          ],
          appsecret: [
            {
              required: true,
              message: '请输入appsecret',
              trigger: 'blur'
            }
          ],
          domain: [
            {
              required: true,
              message: '请输入域名',
              trigger: 'blur'
            }
          ],
        },
        // 保存按钮loading
        loading: false,
        // 是否显示裁剪弹窗
        visible: false
      };
    },
    computed: {
      // 登录用户信息
      loginUser() {
        return this.$store.state.user.info;
      },
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },
    
    methods: {
      /* 保存更改 */
      save() {
        this.$refs.form.validate((valid) => {
          if (!valid) {
            return false;
          }
          this.loading = true;
          const data = {
            ...this.form
          };

          edit(data).then((msg) => {
            this.loading = false;
            this.$message.success(msg);
          }).catch((e) => {
            this.loading = false;
            this.$message.error(e.message);
          });
        });
      },
      /* 重置秘钥 */
      resetKey() {
        this.$confirm('确定要重新生成秘钥吗?', '提示', {
          type: 'warning'
        })
          .then(() => {
            const loading = this.$loading({ lock: true });
            resetKey(0)
              .then((msg) => {
                loading.close();
                this.$message.success(msg);
                this.dataInit();
              })
              .catch((e) => {
                this.$message.error(e.message);
              });
          })
          .catch(() => {});
      },
      dataInit(){
        info(0).then((data) => {
          if(data != null){

            this.$util.assignObject(this.form, {
              ...data
            });

        
          }
          
        }).catch((e) => {
          this.$message.error(e.message);
        });
      },
    }
    ,mounted() {
      this.dataInit();
    },
  };
</script>

<style lang="less" scoped>

  .el-tabs__nav { padding-left: 45%; } 
  .ele-body {
    padding-bottom: 0;
  }

  .ele-bottom-tool .ele-bottom-tool-actions {
    flex: 1;
    text-align: center;
  }

  .title {
    padding-left: 10px;
    border-left: 3px solid #0256ff;
    font-size: 15px;
    line-height: 15px;
    color: #303133;
  }

  .el-card {
    margin-bottom: 15px;
  }

  /* 用户资料卡片 */
  .user-info-card {
    padding: 8px 0;
    text-align: center;

    .user-info-avatar-group {
      position: relative;
      cursor: pointer;
      margin: 0 auto;
      width: 110px;
      height: 110px;
      border-radius: 50%;
      overflow: hidden;

      & > i {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        color: #fff;
        font-size: 30px;
        display: none;
        z-index: 2;
      }

      &:hover {
        & > i {
          display: block;
        }

        &:after {
          content: '';
          position: absolute;
          top: 0;
          left: 0;
          width: 100%;
          height: 100%;
          background-color: rgba(0, 0, 0, 0.3);
        }
      }
    }

    .user-info-avatar {
      width: 110px;
      height: 110px;
      border-radius: 50%;
      object-fit: cover;
    }

    .user-info-name {
      font-size: 24px;
      margin-top: 20px;
    }

    .user-info-desc {
      margin-top: 8px;
    }
  }

  /* 用户信息列表 */
  .user-info-list {
    margin-top: 30px;

    .user-info-item {
      margin-bottom: 16px;
      display: flex;
      align-items: baseline;

      & > i {
        margin-right: 10px;
        font-size: 16px;
      }

      & > span {
        flex: 1;
        display: block;
      }
    }
  }

  /* 用户标签 */
  .user-info-tags .el-tag {
    margin: 10px 10px 0 0;
  }

  /* 用户账号绑定列表 */
  .user-account-list {
    padding: 16px 20px;

    .user-account-item {
      padding: 15px;

      .ele-text-secondary {
        margin-top: 6px;
      }

      .user-account-icon {
        width: 42px;
        height: 42px;
        line-height: 42px;
        text-align: center;
        color: #fff;
        font-size: 26px;
        border-radius: 50%;
        background-color: #3492ed;
        box-sizing: border-box;

        &.el-icon-_wechat {
          background-color: #4daf29;
          font-size: 28px;
        }

        &.el-icon-_alipay {
          background-color: #1476fe;
          padding-left: 5px;
          font-size: 32px;
        }
      }
    }
  }

  
</style>
